import {ref} from 'vue'
import {defineStore} from 'pinia'

export const useUserStore = defineStore('user', () => {
    const userInfo = ref([
        {
            id: 0,
            name: '王小虎',
            status: 1,
            phone: '123456789',
            password: '123456',
            email: '123456789@qq.com',
            address: '上海市普陀区金沙江路 1518 弄'
        },
        {
            id: 1,
            name: '王小龙',
            status: 2,
            phone: '123456789',
            password: '123456',
            email: '123456789@qq.com',
            address: '上海市普陀区金沙江路 1518 弄'
        },
    ])
//增加
    const increment = (item) => {
        userInfo.value.push(item)
        sessionStorage.setItem('userInfo', JSON.stringify(userInfo.value))
    }
    //编辑
    const edit = (item) => {
        userInfo.value.map((v, i) => {
            if (v.id === item.id) {
                userInfo.value[i] = item
            }
        })
        sessionStorage.setItem('userInfo', JSON.stringify(userInfo.value))
    }
//删除
    const del = (id) => {
        userInfo.value.map((v, i) => {
            if (v.id === id) {
                userInfo.value.splice(i, 1)
            }
        })
        sessionStorage.setItem('userInfo', JSON.stringify(userInfo.value))
    }


    return {userInfo, increment, edit, del}
})
